﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BaseHelper.Encrypt
{
    public class DSHelper
    {
        public static string Encode(string txt, int rndChar = 100)
        {
            txt = txt.Trim().Replace("\r", "").Replace("\n", "");
            string strEncrpt = "";
            int rndNum, intChar;
            char[] charWord;
            charWord = txt.ToCharArray();

            Random rnd = new Random();
            for (int i = 0; i < rndChar; i++)
            {
                strEncrpt += rnd.Next(1000).ToString("000000");
            }
            strEncrpt += "#";
            for (int i = 0; i < charWord.Length; i++)
            {
                rndNum = rnd.Next(10);
                intChar = Convert.ToInt32(charWord[i]);
                intChar += rndNum;
                strEncrpt += string.Format("{0}{1}", rndNum.ToString(), intChar.ToString("00000"));
            }
            return strEncrpt;
        }
        public static string Decode(string txt, bool can = false)
        {

            if (txt.Length < 6) return "";
            txt = txt.Trim().Replace("\r", "").Replace("\n", "");
            string strEncrpt = "";
            int rndNum;
            char[] charWord;
            char getChar;
            charWord = txt.ToCharArray();

            if (txt.Contains("#") || can)
            {
                int indexChar;
                if (!can)
                    indexChar = txt.IndexOf('#') + 1;
                else
                    indexChar = 0;
                txt = txt.Substring(indexChar, txt.Length - indexChar);
                for (int i = 0; i < txt.Length; i += 6)
                {
                    rndNum = Convert.ToInt32(txt.Substring(i, 1));
                    getChar = (char)(Convert.ToInt32(txt.Substring(i + 1, 5)) - rndNum);
                    strEncrpt += getChar;
                }
            }
            return strEncrpt;
        }
    }
}
